package controller

import (
	"context"
	"embed"
	"github.com/wailsapp/wails/v2"
	"github.com/wailsapp/wails/v2/pkg/options"
	"github.com/wailsapp/wails/v2/pkg/options/assetserver"
)

// WailsRun 初始化
func WailsRun(assets embed.FS, port int) {
	// 创建控制器实例
	windows := NewWindows()
	config := NewConfig()
	server := NewServer()
	wallpaper := NewWallpaper()
	audio := NewAudio()

	// 启动wails服务
	err := wails.Run(&options.App{
		Title:  "GoDesk：演示wails的使用",
		Width:  860,
		Height: 620,
		AssetServer: &assetserver.Options{
			Assets: assets,
		},
		Frameless:        false,
		BackgroundColour: &options.RGBA{R: 255, G: 255, B: 255, A: 1},
		OnStartup: func(ctx context.Context) {
			//设置 context 对象
			windows.setCtx(ctx)
			config.setCtx(ctx)
			server.setCtx(ctx)
			audio.setCtx(ctx)

			//初始化数据库和表
			server.schema()

			//启动自定义服务
			server.start(port)

			//记录启动日志
			server.log("程序已启动")
		},
		Bind: []interface{}{
			windows,
			config,
			server,
			wallpaper,
			audio,
		},
	})

	if err != nil {
		println("Error:", err.Error())
	}
}
